import grace from "../../utils/grace.js"
const config = require('../../utils/config.js');
grace.page({
      data: {
            from_store_id: 0
      },
      onLoad: function(options) {
            let from_store_id = wx.getStorageSync('from_store_id');
            if (from_store_id) {
                  this.$data.from_store_id = from_store_id;
            }
            this.doLogin();
      },
      doLogin: function() {
            wx.login({
                  success: res => {
                        let param = {
                              code: res.code,
                              api_secret: config.apiSecret
                        };
                        grace.http.post('api/user/applet_login', param).then((res) => {
                              this.setUserInfo(res);
                              if (this.$data.from_store_id) {
                                    this.bindStore(this.$data.from_store_id);
                              }
                              wx.removeStorageSync('from_store_id');
                              grace.bus.$emit("usre_login_success");
                              this.backtoPage();
                        }).catch(err => {
                              if (err.status == 402) {
                                    let res = JSON.parse(err.engine.response);
                                    wx.setStorageSync('openid', res.data.openid);
                                    wx.setStorageSync('session_key', res.data.session_key);
                                    this.$data.$set(this.$data, 'openid', res.data.openid);
                                    this.$data.$set(this.$data, 'session_key', res.data.session_key);
                                    // this.toast('授权失败！请重新点击');
                              }
                        });
                  }
            })
      },
      authSuccess: function(e) {
            let userInfo = e.detail.userInfo;
            let from_store = this.$data.from_store_id;

            let openid = wx.getStorageSync('openid');
            let session_key = wx.getStorageSync('session_key');
            let param = {
                  openid: openid,
                  from_store: from_store,
                  encrypted_data: e.detail.encryptedData,
                  iv: e.detail.iv,
                  session_key: session_key
            };
            this.$http.post('api/user/applet_register', param).then((res) => {
                  this.setUserInfo(res);
                  grace.bus.$emit("usre_login_success");
                  wx.removeStorageSync('from_store_id');
                  this.backtoPage();
            });
      },
      backtoPage: function() {
            let root_page_arr = ['pages/index/index', 'pages/goods/goods', 'pages/cart/cart', 'pages/my/my'];
            let current_page = wx.getStorageSync('current_page');
            if (root_page_arr.indexOf(current_page) >= 0) {
                  wx.switchTab({
                        url: '/' + current_page
                  })
            } else {
                  wx.reLaunch({
                        url: '/' + current_page
                  })
            }
      },
      authFail: function(e) {
            this.toast('授权失败！请重新点击');
      }
});